package com.tjcd.project.library_base.common.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.RequiresApi;

import com.tjcd.project.library_base.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/15
 * @Description 任务流视图
 * @SpittingGrooves
 */
public class FlowView extends View {
    /**
     * 完成的个数
     */
    private int mDoneNum;
    /**
     * 总个数
     */
    private int mFlowNum;
    /**
     * 完成任务的名称
     */
    private List<String> labels = new ArrayList<>();
    /**
     * 圆圈的padding,从圆圈的边界开始
     */
    private float mPaddingCircle;
    /**
     * 圆心的Y坐标
     */
    private float mCenterY;
    private List<Float> mCenterXPosition = new ArrayList<>();

    //连接线的高度
    private float mLineHeight;

    private float mLeftX, mLeftY, mRightX, mRightY;

    private Paint whitePaint = new Paint();

    private float mTextSize;

    private float mTextPadding;

    private float imgRadius;

    public int getDoneNum() {
        return mDoneNum;
    }

    public void setDoneNum(int mDoneNums) {
        this.mDoneNum = mDoneNums;
        invalidate();
    }

    public FlowView(Context context) {
        super(context);
    }

    public FlowView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FlowView);
        mPaddingCircle = ta.getDimension(R.styleable.FlowView_circle_padding, 10);

        mTextPadding = ta.getDimension(R.styleable.FlowView_text_padding, 10);
        mTextSize = ta.getDimension(R.styleable.FlowView_text_size, 20);

        mDoneNum = ta.getInt(R.styleable.FlowView_done_num, 1);
        mFlowNum = ta.getInt(R.styleable.FlowView_flow_num, 4);

        ta.recycle();

        labels.add("资料填写");
        labels.add("证件上传");
        labels.add("人面识别");
        labels.add("注册完成");
    }

    public void setLabels(List<String> show) {
        labels.clear();
        labels.addAll(show);
        invalidate();
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = 200;
//        int width = (int) (mTextPadding * 2 + 2 * mPaddingCircle * mFlowNums + 2 * imgRadius * mFlowNums);
        if (MeasureSpec.EXACTLY == MeasureSpec.getMode(widthMeasureSpec)) {
            width = MeasureSpec.getSize(widthMeasureSpec);
        } else {
            width = Math.min(width, MeasureSpec.getSize(widthMeasureSpec));
        }
        int height = (int) (mTextPadding + 2 * mPaddingCircle + 2 * imgRadius + mTextSize);
        if (MeasureSpec.EXACTLY == MeasureSpec.getMode(heightMeasureSpec)) {
            height = MeasureSpec.getSize(heightMeasureSpec);
        } else {
            height = Math.min(height, MeasureSpec.getSize(heightMeasureSpec));
        }
        setMeasuredDimension(width, height);
    }


    @Override
    public void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //绘制起始Y坐标 从控件高度1/3起
        mCenterY = getHeight() / 3;

        //中间节点图的尺寸，求出半径
        Bitmap standard = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        imgRadius = standard.getWidth() / 2;

        //中间连接线的高度
        Bitmap line = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        mLineHeight = line.getHeight();
        line.getWidth();

        //最左边的点的位置
        mLeftX = 0;
        mLeftY = mCenterY + imgRadius - mLineHeight / 2;
        //最右边的位置
        mRightX = getWidth() - imgRadius;
        mRightY = (float) (getHeight() - Math.max(imgRadius, mLineHeight / 2) + 0.5 * mLineHeight);

        if (labels.size() > 0) {
            //为了防止显示不全，选择第一个字副串的像素长度和圆圈半径中最大的，作为第一个圆心的X坐标
            //开始的点mLeftX，结束的点mRightX
            whitePaint.setTextSize(mTextSize);
            //因为首先会调用onSizeChange()，然后才会调用onDraw，所以要在这里确定字大小。
            float textLength = whitePaint.measureText(labels.get(0));
            //获取label0的字符总长度，像素
            mLeftX = Math.max(textLength / 2, imgRadius);
            //最开始的x坐标是：第一个字副串的像素长度，和圆圈半径中最大的
            float textSize1 = whitePaint.measureText(labels.get(labels.size() - 1));
            //获取label0的字符总长度，像素
            mRightX = getWidth() - Math.max(textSize1 / 2, imgRadius);
            //最开始的x坐标是：第一个字副串的像素长度，和圆圈半径中最大的

            float mLineLength = (mRightX - mLeftX) / (mFlowNum - 1);
            ///两个圆心之间的距离
            mCenterXPosition.add(mLeftX);
            //第一个圆心的x坐标在mRadius处。
            for (int i = 1; i < mFlowNum - 1; i++) {
                mCenterXPosition.add(mLeftX + i * mLineLength);
            }
            mCenterXPosition.add(mRightX);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        whitePaint.setColor(Color.parseColor("#FFFFFF"));
        whitePaint.setAntiAlias(true);
        whitePaint.setStyle(Paint.Style.FILL);
        whitePaint.setStrokeWidth(3);
        whitePaint.setTextSize(mTextSize);
        whitePaint.setTextAlign(Paint.Align.CENTER);

        //画中间的线段
        for (int i = 0; i < mCenterXPosition.size() - 1; i++) {
            if (i < mDoneNum) {
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                canvas.drawBitmap(bitmap, mCenterXPosition.get(i) + imgRadius + mPaddingCircle, mLeftY, new Paint());
            } else {
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                canvas.drawBitmap(bitmap, mCenterXPosition.get(i) + imgRadius + mPaddingCircle, mLeftY, new Paint());
            }
        }
        //画圆圈
        for (int i = 0; i < mCenterXPosition.size(); i++) {
            if (i < mDoneNum) {
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                canvas.drawBitmap(bitmap, mCenterXPosition.get(i) - imgRadius, mCenterY - imgRadius, new Paint());
            } else {
                if (i == 0) {
                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                    canvas.drawBitmap(bitmap, mCenterXPosition.get(i) - imgRadius, mCenterY - imgRadius, new Paint());
                } else if (i == 1) {
                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                    canvas.drawBitmap(bitmap, mCenterXPosition.get(i) - imgRadius, mCenterY - imgRadius, new Paint());
                } else if (i == 2) {
                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                    canvas.drawBitmap(bitmap, mCenterXPosition.get(i) - imgRadius, mCenterY - imgRadius, new Paint());
                } else if (i == 3) {
                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                    canvas.drawBitmap(bitmap, mCenterXPosition.get(i) - imgRadius, mCenterY - imgRadius, new Paint());
                }
            }
        }
        //在圆圈上写label
        for (int i = 0; i < mCenterXPosition.size(); i++) {
            canvas.drawText(labels.get(i),
                    0, labels.get(i).length(),
                    mCenterXPosition.get(i), mCenterY + imgRadius * 2 + getHeight() / 4,
                    whitePaint);
        }
    }
}